/**
 * 
 */
package com.igeekhome.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.igeekhome.mapper.SysPermissionMapper;
import com.igeekhome.dto.SysPermissionDto;
import com.igeekhome.service.ISysPermissionService;
import com.igeekhome.util.NavBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 立以宁
 */
@Service("sysPermissionService")
public class SysPermissionServiceImpl implements ISysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

@Override
public List<NavBean> listNavs(Integer uid) {
	// TODO Auto-generated method stub
	List<SysPermissionDto> per = sysPermissionMapper.selectPermission(uid);

	
	List<NavBean> parent_nav = getChild(per,null);
	for (NavBean nav:parent_nav) {
		List<NavBean> child = getChild(per,nav.getId());

			for (NavBean childNav:child) {
				List<NavBean> grandchild = getChild(per,childNav.getId());
				childNav.setChildren(grandchild);
			}
		nav.setChildren(child);
	}
	return parent_nav;
}

public List<NavBean> getChild(List<SysPermissionDto> per, Integer parentId) {
	List<NavBean> children = new ArrayList<>();
	
	for (SysPermissionDto sys:per) {
		if (sys.getParent_id() == null) {
			if (parentId == null) {
				NavBean nav = new NavBean(sys.getId(), sys.getPower_name(), sys.getIcon(), sys.getUrl_address(), false, null);
				children.add(nav);
			}
		}
		 else if(sys.getParent_id().equals(parentId) ) {
			NavBean nav =  new NavBean(sys.getId(), sys.getPower_name(), sys.getIcon(), sys.getUrl_address(), false, null);
			children.add(nav);
		}
	}
	return children;
}

}
